/**
* \file DGVFile.cpp
*
* \brief This file defines the class for reading and writing data as text files with or without comments using only Qt.
* \author Shekhar S. Chandra, 2008
*
* This file is part of DGV Library.
*
* DGV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DGV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DGV.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "DGVFile.h"

DGVFile::DGVFile()
{
    commentSymbol = "#";
    separator = " ";
    comments = true;
    openedIn = false;
    openedOut = false;
    openedAppend = false;
    verboseMode = false;
}

DGVFile::DGVFile(QString name, QString mode)
{
    commentSymbol = "#";
    separator = " ";
    comments = true;
    openedIn = false;
    openedOut = false;
    openedAppend = false;
    verboseMode = false;

    open(name,mode);
}

DGVFile::~DGVFile()
{
    close();
}

bool DGVFile::open(QString name, QString mode)
{
    fileName = name;
    fileMode = mode;

    if(!fileName.isEmpty())
    {
        file.setFileName(fileName);

        fileMode.toLower();
        if(fileMode == "r")
        {
            if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
                return false;

            in = new QTextStream(&file);

            openedIn = true;
        }
        else if(fileMode == "w")
        {
            if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
                return false;

            out = new QTextStream(&file);

            openedOut = true;
        }
        else if(fileMode == "a")
        {
            if(!file.open(QIODevice::Append | QIODevice::Text))
                return false;

            append = new QTextStream(&file);

            openedAppend = true;
        }
        else
        {
            cerr << "File ERROR: Mode not supported!" << endl;
            return false;
        }
        return true;
    }
    else
        return false;
}

void DGVFile::close()
{
    if(openedIn)
        delete in;
    else if(openedOut)
        delete out;
    else if(openedAppend)
        delete append;

    if(file.isOpen())
        file.close();
    //file.reset();

    openedIn = false;
    openedOut = false;
    openedAppend = false;
}

void DGVFile::writeLine(QString data)
{
    if(!fileName.isEmpty())
    {
        if(openedOut)
        {
            (*out) << data << endl;
        }
        else
            cerr << "File ERROR: File not opened for writing!" << endl;
    }
}

void DGVFile::writeIntegerArrayAsLine(Array<long,1> &data)
{
    QString tmpNum, line;

    for(int j = 0; j < data.size(); j ++)
        line += tmpNum.setNum(data(j)) + separator + " ";

    writeLine(line);
}

void DGVFile::writeDoubleArrayAsLine(Array<double,1> &data)
{
    QString tmpNum, line;

    for(int j = 0; j < data.size(); j ++)
        line += tmpNum.setNum(data(j)) + separator + " ";

    writeLine(line);
}

inline void DGVFile::writeComment(QString comment)
{
    writeLine(commentSymbol + " " + comment);
}

/*void DGVFile::writeIntegerArray(Array<int,1> &data)
{
    if(!fileName.isEmpty())
    {
        if(openedOut)
        {
            (*out) << data << endl;
        }
        else
            cerr << "File ERROR: File not opened for writing!" << endl;
    }
}

void DGVFile::writeArray(Array<imageType,1> &data)
{
    if(!fileName.isEmpty())
    {
        if(openedOut)
        {
            (*out) << data << endl;
        }
        else
            cerr << "File ERROR: File not opened for writing!" << endl;
    }
}

void DGVFile::writeArray(Array<imageType,2> &data)
{
    if(!fileName.isEmpty())
    {
        if(openedOut)
        {
            (*out) << data << endl;
        }
        else
            cerr << "File ERROR: File not opened for writing!" << endl;
    }
}*/

QStringList DGVFile::readNextLine()
{
    QString line;
    QStringList lineElements;

    if(openedIn)
    {
        int commentLocation = 0;

        line = in->readLine();

        ///Check for comments
        ///Ignore if present and told to ignore
        if(line.contains(commentSymbol) && comments)
        {
            commentLocation = line.indexOf(commentSymbol);
            line.truncate(commentLocation); ///Remove comment (everything after comment symbol
        }
    }
    else
        cerr << "File ERROR: File not opened for reading!" << endl;

    //cerr << "Line Read: " << line.toStdString() << endl;
    if(verboseMode)
        lineElements = line.split(separator);
    else
        lineElements = line.split(separator,QString::SkipEmptyParts);

    if(lineElements.size() == 1 && lineElements[0] == separator)
        lineElements.clear(); //Error: Split returns separator if separator not present.

    return lineElements;
}

void DGVFile::readNextLineAsIntegers(Array<long,1> &values)
{
    QStringList lineElements;

    lineElements = readNextLine();

    values.resize(lineElements.size());
    for(int j = 0; j < lineElements.size(); j ++)
        values(j) = lineElements[j].toLong();
}

void DGVFile::readNextLineAsDoubles(Array<double,1> &values)
{
    QStringList lineElements;

    lineElements = readNextLine();

    values.resize(lineElements.size());
    for(int j = 0; j < lineElements.size(); j ++)
        values(j) = lineElements[j].toDouble();
}

bool DGVFile::atEnd()
{
    if(openedIn)
        return in->atEnd();
    else if(openedOut)
        return out->atEnd();
    else if(openedAppend)
        return append->atEnd();
    else
        return file.atEnd();
}
